package com.xhj.sportsnews.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xhj.sportsnews.BR;
import com.xhj.sportsnews.R;
import com.xhj.sportsnews.adapter.RecommendRecycleAdapter;
import com.xhj.sportsnews.bean.Commend;
import com.xhj.sportsnews.callback.HttpService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class News_RecommendFragment extends Fragment implements Callback<Commend> {
private RecyclerView recycleView;
    private RecommendRecycleAdapter adapter;
    private HttpService service;
    private boolean onImages;
    private  List<Commend.DataBean.ContentsBean> contents;
    public News_RecommendFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.fragment_news__recommend, container, false);

        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {
        recycleView= (RecyclerView) rootView.findViewById(R.id.recommend_recycle);
        Map<Class<?>,Pair<Integer,Integer>> map=new HashMap<>();
        map.put(Commend.DataBean.class,Pair.create(R.layout.recommend_headimages, BR.images));
        map.put(Commend.DataBean.TopsBean.class,Pair.create(R.layout.recomend_image, BR.secondima));
        map.put(Commend.DataBean.ContentsBean.class,Pair.create(R.layout.recomend_content, BR.recontent));


        adapter= new RecommendRecycleAdapter(getContext(),new ArrayList(),map);
        recycleView.setAdapter(adapter);
        service= new Retrofit.Builder()
               .baseUrl("http://api.lifxapi.com/tiyu/rest/content/")
               .addConverterFactory(GsonConverterFactory.create())
               .build()
               .create(HttpService.class);
        service.getCommendHead().enqueue(this);
    }

    @Override
    public void onResponse(Call<Commend> call, Response<Commend> response) {
            adapter.add(0,response.body().getData());
            adapter.addAll(1,response.body().getData().getTops());

        contents = response.body().getData().getContents();

        adapter.addAll(response.body().getData().getTops().size()+1,contents);

    }

    @Override
    public void onFailure(Call<Commend> call, Throwable t) {

    }
}
